import MatchGameConfig from "./MatchGameConfig";

const { ccclass, property } = cc._decorator;

@ccclass
class SkillAniCtrl {
    @property(cc.Prefab)
    skillPrefab = null;
    @property(cc.Node)
    skillParent = null;
    hammerSpData = null;
    gunSpData = null;
    arrowSpData = null;
    effectParent = null;
    isReady = false;

    static _instance = null;

    static getInstance():SkillAniCtrl {
        if (!this._instance) {
            this._instance = new SkillAniCtrl();
        }
        return this._instance;
    }

    ready(e) {
        this.isReady = true;
        this.effectParent = e;
    }

    async init() {
        await this.loadSkillPrefab();
        await this.loadSkillSpData();
    }

    loadSkillPrefab() {
        return new Promise((resolve, reject) => {
            cc.resources.load("prefabs/skillPrefab", cc.Prefab, (err, prefab) => {
                if (err) {
                    console.error("Failed to load skillPrefab", err);
                    reject(err);
                } else {
                    this.skillPrefab = prefab;
                    resolve(prefab);
                }
            });
        });
    }

    loadSkillSpData() {
        return new Promise((resolve, reject) => {
            cc.resources.loadDir("spine/skill", sp.SkeletonData, (err, datas) => {
                if (err) {
                    console.error("Failed to load skill animation data", err);
                    reject(err);
                } else {
                    datas.forEach(data => {
                        switch (data.name) {
                            case "ChuiZi":
                                this.hammerSpData = data;
                                break;
        
                            case "DaPao":
                                this.gunSpData = data;
                                break;
        
                            case "GongJian":
                                this.arrowSpData = data;
                                break;
                        }
                    });
                    
                    if (this.hammerSpData && this.gunSpData && this.arrowSpData) {
                        resolve(null);
                    } else {
                        reject(new Error("Failed to load skill animation data"));
                    }
                }
            });
        });
    }

    createSkillItem(e, t, a) {
        var o = cc.instantiate(this.skillPrefab);
        o.parent = this.effectParent;
        o.setPosition(this.getV3Pos(e.row, e.col));
        var i = o.getComponent(sp.Skeleton);
        o.angle = 0;
        i.skeletonData = t;
        i.premultipliedAlpha = false;
        i.setCompleteListener(() => {
            i.setCompleteListener(null);
            i.skeletonData = null;
            o.destroy();
            a && a();
        });
        return i;
    }

    playHammer(e, t) {
        this.createSkillItem(e, this.hammerSpData, t).setAnimation(0, "Skill", false);
    }

    playGunAni(e, t) {
        var a = this.createSkillItem(
            { row: e.row, col: e.col + 1 },
            this.gunSpData,
            t
        );
        a.setCompleteListener(null);
        a.node.y += 50;
        a.setAnimation(0, "skill", false);
        setTimeout(() => {
            a.setCompleteListener(() => {
                a.setCompleteListener(null);
                a.skeletonData = null;
                a.node.destroy();
                t && t();
            });
        }, 500);
    }

    playArrowAni(e, t) {
        this.createSkillItem({ row: 4, col: e.col }, this.arrowSpData, t).setAnimation(0, "skill", false);
    }

    getV3Pos(e, t) {
        return cc.v3(e * MatchGameConfig.ITEM_WIDTH , -t *MatchGameConfig.ITEM_HEIGHT);
    }
}

export default  SkillAniCtrl.getInstance();